home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Fix.h
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
5KB
|
187 lines
#include <math.h>
#define PI 3.1415926535897932
#define angle2fix 40.74366543153
#define fix2angle 0.02454369260617
extern int cosine_table[], arccosine_table[], tangent_table[];
class fix
{
private:
int v;
static int itofix(int x)
{
return x << 8;
}
static int fixtoi(int x)
{
return (x >> 8) + ((x & 0x80) >> 7);
}
static int ftofix(double x)
{
return (int)(x * 256.0 + (x < 0 ? -0.5 : 0.5));
}
static double fixtof(int x)
{
return (double)x / 256.0;
}
static int fmul(int x, int y)
{
int t;
__asm
{
mov eax, x
mov ecx, y
imul ecx
shrd eax, edx, 8
mov t, eax
}
return t;
}
static int fdiv(int x, int y)
{
int t;
__asm
{
mov eax, x
mov edx, eax
shl eax, 8
sar edx, 24
mov ecx, y
idiv ecx
mov t, eax
}
return t;
}
public:
fix() { }
fix(const fix &x) { v = x.v; }
fix(const int x) { v = itofix(x); }
fix(const long x) { v = itofix(x); }
fix(const unsigned int x) { v = itofix(x); }
fix(const unsigned long x) { v = itofix(x); }
fix(const double x) { v = ftofix(x); }
operator int() const { return fixtoi(v); }
operator long() const { return fixtoi(v); }
operator unsigned int() const { return fixtoi(v); }
operator unsigned long() const { return fixtoi(v); }
operator double() const { return fixtof(v); }
fix &operator = (const fix &x) { v = x.v; return *this; }
fix &operator = (const int x) { v = itofix(x); return *this; }
fix &operator = (const long x) { v = itofix(x); return *this; }
fix &operator = (const unsigned int x) { v = itofix(x); return *this; }
fix &operator = (const unsigned long x) { v = itofix(x); return *this; }
fix &operator = (const double x) { v = ftofix(x); return *this; }
fix &operator += (const fix x) { v += x.v; return *this; }
fix &operator -= (const fix x) { v -= x.v; return *this; }
fix &operator *= (const fix x) { v = fmul(v, x.v); return *this; }
fix &operator *= (const int x) { v *= x; return *this; }
fix &operator /= (const fix x) { v = fdiv(v, x.v); return *this; }
fix &operator /= (const int x) { v /= x; return *this; }
fix &operator <<= (const int x) { v <<= x; return *this; }
fix &operator >>= (const int x) { v >>= x; return *this; }
fix &operator &= (const int x) { v &= (x << 8) + 0xff; return *this; }
fix &operator ++ () { v += itofix(1); return *this; }
fix &operator -- () { v -= itofix(1); return *this; }
fix operator - () { fix t; t.v = -v; return t; }
friend fix operator + (const fix x, const fix y) { fix t; t.v = x.v + y.v; return t; }
friend fix operator - (const fix x, const fix y) { fix t; t.v = x.v - y.v; return t; }
friend fix operator * (const fix x, const fix y) { fix t; t.v = fmul(x.v, y.v); return t; }
friend fix operator * (const fix x, const int y) { fix t; t.v = x.v * y; return t; }
friend fix operator * (const int x, const fix y) { fix t; t.v = y.v * x; return t; }
friend fix operator / (const fix x, const fix y) { fix t; t.v = fdiv(x.v, y.v); return t; }
friend fix operator / (const fix x, const int y) { fix t; t.v = x.v / y; return t; }
friend fix operator << (const fix x, const int y) { fix t; t.v = x.v << y; return t; }
friend fix operator >> (const fix x, const int y) { fix t; t.v = x.v >> y; return t; }
friend int operator == (const fix x, const fix y) { return (x.v == y.v); }
friend int operator != (const fix x, const fix y) { return (x.v != y.v); }
friend int operator < (const fix x, const fix y) { return (x.v < y.v); }
friend int operator > (const fix x, const fix y) { return (x.v > y.v); }
friend int operator <= (const fix x, const fix y) { return (x.v <= y.v); }
friend int operator >= (const fix x, const fix y) { return (x.v >= y.v); }
friend fix cos(fix x)
{
fix t;
t.v = cosine_table[((x.v & 0x40)? (x.v >> 7) + 1 : (x.v >> 7)) & 0x1ff];
return t;
}
friend fix sin(fix x)
{
fix t;
t.v = cosine_table[(((x.v & 0x40)? (x.v >> 7) + 1 : (x.v >> 7)) - 128) & 0x1ff];
return t;
}
friend fix tan(fix x)
{
fix t;
t.v = tangent_table[((x.v & 0x40)? (x.v >> 7) + 1 : (x.v >> 7)) & 0xff];
return t;
}
friend fix acos(fix x)
{
fix t;
t.v = x.v < -256 || x.v > 256? 0 : arccosine_table[x.v + 256];
return t;
}
friend fix asin(fix x)
{
fix t;
t.v = x.v < -256 || x.v > 256? 0 : 0x4000 - arccosine_table[x.v + 256];
return t;
}
friend fix atan(fix x)
{
return (fix)(angle2fix * atan((double)x));
}
friend fix atan2(fix x, fix y)
{
return (fix)(angle2fix * atan2((double)x, (double)y));
}
friend fix sqrt(fix x)
{
return (fix)sqrt((double)x);
}
friend fix abs(fix x)
{
return x < (fix)0? -x:x;
}
};